package com.efounder.ospopenplanet.wxapi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.frame.language.MultiLanguageUtil;
import com.efounder.utils.ResStringUtil;
import com.pansoft.mobilemallpay.R;
import com.pansoft.mobilemallpay.pays.WxPay;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 每个app都必须有这样一个类，作为支付结果显示
 * 包名与app包名一致
 * @author will
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler, View.OnClickListener {
    private static final String TAG = "WXPayEntryActivity";
    public static final String FINISH_ORDER_ACTIVITY = "FINISHORDERACTIVITY";
    public static final String PAY_SUCCESS = "PAYSUCCESS";
    private IWXAPI mWxApi;
    private Intent intent;
    private Button orderPayBtn;
    /** //支付失败 0 支付成功1*/
    private int payStatus = 0;
    private TextView title;
    private ImageView statusImage;
    private TextView money;
    private TextView description;
    private TextView shoukuanfang;
    private String googName;
    private String descriptionStr;
    private String price;


    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(MultiLanguageUtil.setLocal(newBase));
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tit);
        googName = EnvironmentVariable.getProperty("mobile_pay_good_name");
        descriptionStr = EnvironmentVariable.getProperty("mobile_pay_good_description");
        price = EnvironmentVariable.getProperty("mobile_pay_good_price");
        initView();
        String type = getIntent().getStringExtra("type");
        if ("ali".equals(type)) {
            //说明是阿里支付跳转进来的
            String status = getIntent().getStringExtra("status");
            if ("success".equals(status)) {
                //支付成功
                payStatus = 1;
                changeView();
                notifySuccess();
            } else {
                payStatus = 0;
                changeView();

            }

        }
        mWxApi = WXAPIFactory.createWXAPI(this, "wx2f4c2daedd19b1a9");
        mWxApi.handleIntent(getIntent(), this);


    }

    private void changeView() {
        if (payStatus == 1) {
            //用户支付成功;
            title.setText(R.string.mobile_mall_pay_success);
            title.setTextColor(getResources().getColor(R.color.mobile_mall_color_accent));
            statusImage.setImageResource(R.drawable.transfer_success);
            shoukuanfang.setText(googName);
            description.setText(descriptionStr);
        } else {
            //支付失败;
            //支付失败,退回去让用户重新支付;
            title.setText(R.string.mobile_mall_pay_fail);
            title.setTextColor(getResources().getColor(R.color.mobile_mall_color_orange));
            statusImage.setImageResource(R.drawable.transfer_fail);

            orderPayBtn.setText(R.string.mobile_mall_pay_repay);
        }
        shoukuanfang.setText(googName);
        description.setText(descriptionStr);

    }

    private void initView() {

        orderPayBtn = (Button) findViewById(R.id.orderPayBtn);
        title = (TextView) findViewById(R.id.title);
        statusImage = ((ImageView) findViewById(R.id.statusImage));
        money = (TextView) findViewById(R.id.orderMoney);
        description = (TextView) findViewById(R.id.description);
        shoukuanfang = (TextView) findViewById(R.id.shoukuanfang);
        findViewById(R.id.backButton_title_my_live).setOnClickListener(this);
        orderPayBtn.setOnClickListener(this);
        findViewById(R.id.bar).setBackgroundColor(getResources().getColor(R.color.chat_red));
        money.setText(price);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mWxApi.handleIntent(intent, this);
    }


    @Override
    public void onReq(BaseReq baseReq) {
        Log.d(TAG, "请求发出的回调");
    }

    @Override
    public void onResp(BaseResp baseResp) {
        int code = baseResp.errCode;
        intent = new Intent(WxPay.ACTIONG_MESSAGE);
        if (code == 0) {
            intent.putExtra("msg", ResStringUtil.getString(R.string.mobile_mall_pay_success));
            intent.putExtra("code", "0");
            payStatus = 1;
            notifySuccess();
        } else if (code == -2) {
            payStatus = 0;
            intent.putExtra("msg", ResStringUtil.getString(R.string.mobile_mall_pay_canceled_hint));
            intent.putExtra("code", "-2");
        } else {
            intent.putExtra("msg", ResStringUtil.getString(R.string.mobile_mall_pay_fail));
            payStatus = 0;
            intent.putExtra("code", "-1");
        }

        changeView();
    }

    @Override
    public void onBackPressed() {
        if (payStatus == 1) {
            finishLastActivity();
        }
        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.orderPayBtn) {
            if (payStatus == 0) {
                finish();
            } else {
                //支付成功;
                //结束当前界面和上一个界面;
                finishLastActivity();
                finish();
            }
        } else if (v.getId() == R.id.backButton_title_my_live) {
            onBackPressed();
        }
    }

    private void notifySuccess() {
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(PAY_SUCCESS));
    }

    private void finishLastActivity() {
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(FINISH_ORDER_ACTIVITY));
    }
}
